package org.daidai.abstrusewriting.homepage.hot.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Parcelable;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;
import android.widget.TableLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.mob.commons.SHARESDK;
import com.squareup.picasso.Picasso;

import org.daidai.abstrusewriting.R;
import org.daidai.abstrusewriting.homepage.hot.adapters.ViewpageAdapter;
import org.daidai.abstrusewriting.homepage.hot.client.ClientApi;
import org.daidai.abstrusewriting.homepage.hot.fragments.BaseFragment;
import org.daidai.abstrusewriting.homepage.hot.fragments.DetailFragment;
import org.daidai.abstrusewriting.homepage.hot.fragments.ProgramFragment;
import org.daidai.abstrusewriting.homepage.hot.model.GuessItem;
import org.daidai.abstrusewriting.homepage.hot.model.Item;
import org.daidai.abstrusewriting.homepage.hot.model.RecommendAlbums;
import org.daidai.abstrusewriting.homepage.hot.model.RecommendDetail;
import org.daidai.abstrusewriting.homepage.hot.model.RecommendItemDetail;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.w3c.dom.Text;

import java.util.ArrayList;

import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.onekeyshare.OnekeyShare;

public class EditRecommendActivity extends AppCompatActivity {

    private ImageView mpicture;
    private TextView mtitle;
    private TextView mplayname;
    private TextView mplaynumber;
    private TextView mdivide;
    private String mAlbumId;
    private DetailFragment mDetailFragment;
    private ProgramFragment mProgramFragment;
    private RecommendDetail mRecommendDetail;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_recommend);
        Toolbar toolbar = (Toolbar) findViewById(R.id.activity_edit_recommend_toolbar);
        setSupportActionBar(toolbar);
        ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);
        mpicture = (ImageView) findViewById(R.id.editrecommend_pic);
        mtitle = ((TextView) findViewById(R.id.editrecommend_title));
        mplayname = (TextView) findViewById(R.id.editrecommend_playname);
        mplaynumber = (TextView) findViewById(R.id.editrecommend_playnumber);
        mdivide = (TextView) findViewById(R.id.editrecommend_divide);
        TabLayout tabLayout = (TabLayout) findViewById(R.id.activity_edit_recommend_tablayout);
        //设置Viewpager
        ViewPager viewPager = (ViewPager) findViewById(R.id.activity_edit_recommend_viewpager);
        ArrayList<BaseFragment> fragments = new ArrayList<>();
        mDetailFragment = new DetailFragment();
        mProgramFragment = new ProgramFragment();
        fragments.add(mDetailFragment);
        fragments.add(mProgramFragment);
        ViewpageAdapter adapter = new ViewpageAdapter(getSupportFragmentManager(),fragments);
        viewPager.setAdapter(adapter);
        tabLayout.setupWithViewPager(viewPager);
        ///
        Intent intent = getIntent();
        Parcelable data = intent.getParcelableExtra("data");
        if (data instanceof RecommendAlbums) {
            RecommendAlbums recommendAlbums =    (RecommendAlbums) data;
            mAlbumId = recommendAlbums.getAlbumId();
            //将id传递给ProgremFragment
            Bundle bundle = new Bundle();
            bundle.putString("mAlbumId", mAlbumId);
            mProgramFragment.setArguments(bundle);
            mtitle.setText(recommendAlbums.getTrackTitle());
            mplayname.setText("主播:"+recommendAlbums.getNickname());
            mplaynumber.setText("类别:"+recommendAlbums.getPlaysCounts());
            mdivide.setText("播放量:"+ recommendAlbums.getTags());
            Picasso.with(this).load(recommendAlbums.getCoverMiddle())
                    .config(Bitmap.Config.ARGB_8888)
                    .into(mpicture);
        }else if (data instanceof GuessItem){
            GuessItem guessItem = (GuessItem) data;
            mAlbumId = guessItem.getAlbumId();
            Bundle bundle = new Bundle();
            bundle.putString("mAlbumId", mAlbumId);
            mProgramFragment.setArguments(bundle);
            mtitle.setText(guessItem.getTrackTitle());
            mplayname.setText("主播:"+guessItem.getNickname());
            mplaynumber.setText("类别:"+guessItem.getPlaysCounts());
            mdivide.setText("播放量:"+ guessItem.getTags());
            Picasso.with(this).load(guessItem.getCoverMiddle())
                    .config(Bitmap.Config.ARGB_8888)
                    .into(mpicture);
        }else if (data instanceof Item) {
            Item item = (Item) data;
            mAlbumId = item.getAlbumId();
            Bundle bundle = new Bundle();
            bundle.putString("mAlbumId", mAlbumId);
            mProgramFragment.setArguments(bundle);
            mtitle.setText(item.getTrackTitle());
            mplayname.setText("主播:" + item.getNickname());
            mplaynumber.setText("类别:" + item.getPlaysCounts());
            mdivide.setText("播放量:" + item.getTags());
            Picasso.with(this).load(item.getCoverMiddle())
                    .config(Bitmap.Config.ARGB_8888)
                    .into(mpicture);
        }
        else if (data instanceof RecommendItemDetail){
            RecommendItemDetail itemDetail = (RecommendItemDetail) data;
            mAlbumId = itemDetail.getAlbumId();
            Bundle bundle = new Bundle();
            bundle.putString("mAlbumId", mAlbumId);
            mProgramFragment.setArguments(bundle);
            mtitle.setText(itemDetail.getTitle());
            mplayname.setText("主播:");
            mplaynumber.setText("类别:"+itemDetail.getPlaysCounts());
            mdivide.setText("播放量:");
            Picasso.with(this).load(itemDetail.getCoverSmall())
                    .config(Bitmap.Config.ARGB_8888)
                    .into(mpicture);

        }


    }

    @Override
    protected void onResume() {
        super.onResume();
        ClientApi.getRecommendDetail("http://mobile.ximalaya.com/mobile/v1/album/detail?albumId="+mAlbumId);
    }

    @Override
    protected void onDestroy() {
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.detail_menu,menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int itemId = item.getItemId();
        switch (itemId){
            case R.id.menu_help:
                Toast.makeText(this, "帮助", Toast.LENGTH_SHORT).show();
                break;
            case R.id.menu_share:
                ShareSDK.initSDK(this);
                OnekeyShare oks = new OnekeyShare();
                //关闭sso授权
                oks.disableSSOWhenAuthorize();
                oks.setTitle("标题");
                // titleUrl是标题的网络链接，仅在人人网和QQ空间使用
                oks.setTitleUrl("http://sharesdk.cn");
                // text是分享文本，所有平台都需要这个字段
                oks.setText("我是分享文本");
                //分享网络图片，新浪微博分享网络图片需要通过审核后申请高级写入接口，否则请注释掉测试新浪微博
                oks.setImageUrl("http://f1.sharesdk.cn/imgs/2014/02/26/owWpLZo_638x960.jpg");
                // imagePath是图片的本地路径，Linked-In以外的平台都支持此参数
                //oks.setImagePath("/sdcard/test.jpg");//确保SDcard下面存在此张图片
                // url仅在微信（包括好友和朋友圈）中使用
                oks.setUrl("http://sharesdk.cn");
                // comment是我对这条分享的评论，仅在人人网和QQ空间使用
                oks.setComment("我是测试评论文本");
                // site是分享此内容的网站名称，仅在QQ空间使用
                oks.setSite("ShareSDK");
                // siteUrl是分享此内容的网站地址，仅在QQ空间使用
                oks.setSiteUrl("http://sharesdk.cn");
                // 启动分享GUI
                oks.show(this);
                break;
            case android.R.id.home:
                onBackPressed();
                break;

        }
        return super.onOptionsItemSelected(item);
    }
}
